function varargout = gui_directories(varargin)
% GUI_DIRECTORIES MATLAB code for gui_directories.fig
%      GUI_DIRECTORIES, by itself, creates a new GUI_DIRECTORIES or raises the existing
%      singleton*.
%
%      H = GUI_DIRECTORIES returns the handle to a new GUI_DIRECTORIES or the handle to
%      the existing singleton*.
%
%      GUI_DIRECTORIES('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_DIRECTORIES.M with the given input arguments.
%
%      GUI_DIRECTORIES('Property','Value',...) creates a new GUI_DIRECTORIES or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_directories_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_directories_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gui_directories

% Last Modified by GUIDE v2.5 17-Jan-2013 11:23:38

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_directories_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_directories_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before gui_directories is made visible.
function gui_directories_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to gui_directories (see VARARGIN)

% Choose default command line output for gui_directories
try
    handles.output = 0;

    % Update handles structure
    guidata(hObject, handles);

    gui_main = find(strcmp(varargin, 'gui_main'));
    if (isempty(gui_main)) || ...
            (length(varargin) <= gui_main) || ...
            (~ishandle(varargin{gui_main+1}))
        dontOpen = true;
    else
        handles.gui_main = varargin{gui_main+1};
        handlesMain = guidata(handles.gui_main);
        handles.project = handlesMain.project;
        guidata(hObject, handles);

        if(isempty(handles.project.scratch_directory))
            set(handles.scratch_text, 'String',...
                handlesMain.pref.scratch_directory);
        else        
            set(handles.scratch_text, 'String',...
                handles.project.scratch_directory);
        end

        if(isempty(handles.project.scratch_directory2))
            set(handles.scratch2_text, 'String',...
                handlesMain.pref.scratch_directory2);
        else        
            set(handles.scratch2_text, 'String',...
                handles.project.scratch_directory2);
        end

        % UIWAIT makes gui_project_settings wait for user response (see UIRESUME)
        uiwait(hObject);
    end
catch exception
    gui_exception(exception);
end


% --- Outputs from this function are returned to the command line.
function varargout = gui_directories_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
delete(hObject);


function scratch_text_Callback(hObject, eventdata, handles)
% hObject    handle to scratch_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of scratch_text as text
%        str2double(get(hObject,'String')) returns contents of scratch_text as a double


% --- Executes during object creation, after setting all properties.
function scratch_text_CreateFcn(hObject, eventdata, handles)
% hObject    handle to scratch_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function scratch2_text_Callback(hObject, eventdata, handles)
% hObject    handle to scratch2_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of scratch2_text as text
%        str2double(get(hObject,'String')) returns contents of scratch2_text as a double


% --- Executes during object creation, after setting all properties.
function scratch2_text_CreateFcn(hObject, eventdata, handles)
% hObject    handle to scratch2_text (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in scratch_browse_button.
function scratch_browse_button_Callback(hObject, eventdata, handles)
% hObject    handle to scratch_browse_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    dirname = uigetdir(get(hObject,'String'),'Scratch Directory');
    if isequal(dirname, 0)
        return
    else
        % Append a file separator (i.e., \ for Windows or / for Linux) 
        % to the directory name in case it does not have one.
        if (strcmpi(dirname(end), filesep) == false)
            dirname = [dirname filesep];
        end
        set(handles.scratch_text,'String',dirname);
        handles.project.scratch_directory = dirname;
        guidata(hObject, handles);
    end 
catch exception
    gui_exception(exception);
end


% --- Executes on button press in scratch2_browse_button.
function scratch2_browse_button_Callback(hObject, eventdata, handles)
% hObject    handle to scratch2_browse_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    dirname = uigetdir(get(hObject,'String'),'Scratch Directory');
    if isequal(dirname, 0)
        return
    else
        % Append a file separator (i.e., \ for Windows or / for Linux) 
        % to the directory name in case it does not have one.
        if (strcmpi(dirname(end), filesep) == false)
            dirname = [dirname filesep];
        end
        set(handles.scratch2_text,'String',dirname);
        handles.project.scratch_directory2 = dirname;
        guidata(hObject, handles);
    end 
catch exception
    gui_exception(exception);
end


% --- Executes on button press in ok_button.
function ok_button_Callback(hObject, eventdata, handles)
% hObject    handle to ok_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    mainHandles = guidata(handles.gui_main);
    handles.project.scratch_directory = get(handles.scratch_text, 'String');
    handles.project.scratch_directory2 = get(handles.scratch2_text, 'String');

    if(get(handles.save_as_default_checkbox, 'Value'))
        if(isempty(handles.project.scratch_directory) || ...
                isempty(handles.project.scratch_directory2) )
            warndlg('When giving default settings, both scratch directory fields are mandatory');
            return;
        end
    end
    
    mainHandles.project = handles.project;
    guidata(handles.gui_main, mainHandles);

    if(get(handles.save_as_default_checkbox, 'Value'))
        mainHandles.pref.scratch_directory = ...
            handles.project.scratch_directory;
        mainHandles.pref.scratch_directory2 = ...
            handles.project.scratch_directory2;

        guidata(handles.gui_main, mainHandles);

        % write the new settings to the userfile
        pref = mainHandles.pref;
        save(mainHandles.userfile, 'pref');
    end
    
    handles.output = 1;
    guidata(hObject, handles);
    gui_close_figure(handles.directories_figure);
catch exception
    gui_exception(exception);
end

% --- Executes during object creation, after setting all properties.
function directories_figure_CreateFcn(hObject, eventdata, handles)
% hObject    handle to directories_figure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes when user attempts to close directories_figure.
function directories_figure_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to directories_figure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
try
    gui_close_figure(handles.directories_figure);
catch exception
    gui_exception(exception);
end


% --- Executes on button press in cancel_button.
function cancel_button_Callback(hObject, eventdata, handles)
% hObject    handle to cancel_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
try
    gui_close_figure(handles.directories_figure);
catch exception
    gui_exception(exception);
end


% --- Executes on button press in save_as_default_checkbox.
function save_as_default_checkbox_Callback(hObject, eventdata, handles)
% hObject    handle to save_as_default_checkbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of save_as_default_checkbox
